home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / ai / pdprolog.lzh / PROLOG.DOC < prev    next >
Text File  |  1987-10-31  |  87KB  |  3,037 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                      A.D.A PROLOG Documentation Version 1.91
  15.                   for the Educational and Public Domain Versions
  16.  
  17.                                   May 28, 1986
  18.  
  19.               Copyright Robert Morein and Automata Design Associates
  20.  
  21.                                  1570 Arran Way
  22.                                Dresher, Pa. 19025
  23.  
  24.                                  (215)-646-4894
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                                       News
  71.  
  72.              Release  1.91  of PD PROLOG accesses an editor.  The  editor 
  73.         "PROLOGED.COM",  by Li Su of Richfield,  Minnesota,  is a simple, 
  74.         and compact. Three cheers for Li Su!
  75.  
  76.  
  77.                                 Copyright Notice
  78.  
  79.              The  public domain PD PROLOG system has been contributed  to 
  80.         the  public domain for unrestricted use with one  exception:  the 
  81.         object  code  may not be  disassembled  or  modified.  Electronic 
  82.         bulletin  boards  and SIG groups are urged to aid in giving  this 
  83.         software the widest possible distribution.
  84.  
  85.              This documentation may be reproduced freely,  but it may not 
  86.         be  included in any other documentation without the permission of 
  87.         the author.
  88.  
  89.                                   Introduction
  90.                 
  91.         We  hope  that you'll get some fun out of this  PROLOG.  It  will 
  92.         afford  you exposure to THE fifth generation language at the cost 
  93.         only  of  some  intellectual  effort.  The  motive  is  perfectly 
  94.         explicable:  We  want you to think of Automata Design  Associates 
  95.         for  fifth  generation  software.  It also gives us a  nice  warm 
  96.         feeling.
  97.  
  98.         The  minimum  memory  requirement is 200 k of  transient  program 
  99.         area,  plus  whatever  space is needed to execute  programs  from 
  100.         within PROLOG.  DOS or MSDOS 2.0 are required.  The program  does 
  101.         not  require  IBM PC compatibility to run,  although  the  screen 
  102.         access routines do require compatibility.
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.                                         2
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                      Products by Automata Design Associates
  137.  
  138.              Automata  Design  Associates  specializes  in  software  for 
  139.         artificial  intelligence  and  robotic  applications.   A  PROLOG 
  140.         language  system is available in various configurations.  A  LISP  
  141.         interpreter will be introduced in March of 1985.
  142.  
  143.  
  144.              Six  PROLOG  Development  Systems  available  from  Automata 
  145.         Design Associates. 
  146.  
  147.              The   ADA   PROLOG   Runtime   Systems   permit   no-royalty 
  148.         distribution of a complete logic system with your  product.  Each 
  149.         system   is   functionally   equivalent  to   the   corresponding 
  150.         Development System,  except that the user command loop is  absent 
  151.         and tracing is not implemented.
  152.  
  153.          The  systems  run  under  the MSDOS or  PCDOS,  and  access  all 
  154.         available memory up to 1 megabyte.
  155.  
  156.              .Public Domain PROLOG
  157.  
  158.         This  serves to further the general awareness of the public about 
  159.         PROLOG.  It  also is an excellent adjunct to anyone learning  the 
  160.         language.  Most  of  the core PROLOG described  by  Clocksin  and 
  161.         Mellish  in  the book Programming In  PROLOG  is  implemented.  A 
  162.         complete  IBM PC video screen support library is included in this 
  163.         and  all other A.D.A.  prologs.  Trace predicates are  not.  This 
  164.         version is available from us for $10.00 postage paid.
  165.  
  166.  
  167.              .Educational PROLOG
  168.  
  169.         At  extremely modest cost this affords an educational institution 
  170.         or  individual  a  PROLOG  system  which  provides  the   maximum 
  171.         available  programming  area  available  within  the  8086  small 
  172.         programming model.  Tracing,  a debugging aid,  allows monitoring 
  173.         a program as it runs.  User settable spy points selectively allow 
  174.         this.  Exhaustive  tracing  is also  available.  I/O  redirection 
  175.         gives some file ability.
  176.  
  177.              An  "exec"  function allows the execution of  a  program  or 
  178.         editor  from  within  PROLOG,  thus  encouraging  an  interactive 
  179.         environment.
  180.  
  181.              An  "interrupt"   menu is added,  permitting the control  of 
  182.         tracing, toggling the printer, and screen printing.
  183.  
  184.              Definite clause grammar support is now included.
  185.              
  186.              Educational PROLOG Development  System: $29.95.
  187.              Educational PROLOG Runtime System:      $29.095
  188.  
  189.  
  190.  
  191.  
  192.  
  193.                                         3
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.              .FS PROLOG
  203.  
  204.              A  small  increment  in price adds full random  access  file 
  205.         capability. Character and structure I/O are allowed.
  206.  
  207.              The  "asserta and "assertz" predicates are expanded and work 
  208.         with a clause indexing ability.  One can assert clauses  anywhere 
  209.         in the database under precise pattern matching control. 
  210.  
  211.              A tree structured lexical scoping system and floating  point 
  212.         arithmetic are other enhancements.
  213.  
  214.              FS PROLOG Development System: $49.95
  215.              FS PROLOG Runtime System:     $49.95
  216.  
  217.  
  218.              .VMI PROLOG -- Virtual Memory (Replaces type VMS)
  219.  
  220.  
  221.              At reasonable cost the addition of virtual memory gives  an 
  222.         expansion of capabilities of an order of magnitude. 
  223.  
  224.              The  database on disk is treated transparently.  No  special 
  225.         provisions  need  be  made  by the  user.  Virtual  and  resident 
  226.         databases  may be mixed.  A unique updating algorithim  preserves 
  227.         the format of the database as typed by the user while making only 
  228.         those changes necessary to make it equivalent to the database  in 
  229.         central memory.
  230.  
  231.              VMI PROLOG Development System: $99.95
  232.              VMI PROLOG Runtime System:     $99.95
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.                                         4
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.              .VML PROLOG Large model Virtual Memory System
  269.  
  270.              A.D.A.  PROLOG is a remarkable fifth generation developement 
  271.         tool  for  the  implementation  of  intelligent  strategies   and 
  272.         optimized  control.  It  is both the kernel for  applications  of 
  273.         virtually  unlimited scope and a sophisticated developement  tool 
  274.         that multiplies the productivity of the programmer many times. 
  275.  
  276.              With  a  cost/performance ratio exceeding that of any  other 
  277.         product  and  a  compatibility  insured  by  compliance  to   the 
  278.         Edinburgh syntax, performance is enhanced by numerous extensions, 
  279.         many of them invisible to the user.
  280.  
  281.              A quick overview of some of the features discloses:
  282.  
  283.              1)  Invisible  compilation  to  a  semantic  network 
  284.              preserves the flexibility of the interpreted mode and 
  285.              the speed of a compiler.
  286.  
  287.              The  programmer can compile and recompile any portion 
  288.              of a module at any time.  The edit/compile/test cycle 
  289.              is short and free of strain. An interface is provided 
  290.              to an editor of choice.
  291.  
  292.  
  293.              2) Floating point arithmetic with a full  complement 
  294.              of  input  and  output  methods,  transcendental  and 
  295.              conversion functions.
  296.  
  297.  
  298.              3)  Virtual  memory.  Module  size  and  number  are 
  299.              unrestricted,   with  a  total  capacity  of  several 
  300.              hundred megabytes.  Resident and virtual modules  may 
  301.              be co-resident. Compilation is incremental. The cache 
  302.              algorithim  is  sophisticated.  Changes made  in  the 
  303.              database can be updated to disk by a single command.
  304.  
  305.  
  306.              4) A powerful exec function and acceptance of  stream 
  307.              input make  integration into  applications practical.
  308.  
  309.  
  310.              5)  Global polymorphic variables  retain  information 
  311.              that  would  otherwise  require  the  "assertion"  of 
  312.              facts.
  313.  
  314.  
  315.              6)  A  quoted  variable class,  borrowed  from  LISP, 
  316.              permits  referencing variables as objects as well  as 
  317.              by value.
  318.  
  319.              7) Multidimensional arrays,  dynamically created  and 
  320.              destroyed,  efficiently  store numeric and nonnumeric 
  321.              structures. Arrays are ideal for representing spatial 
  322.              and ordinal relationships.
  323.  
  324.  
  325.                                         5
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.              8)  Debugging facilities let you see your program run 
  335.              without any additional generation steps.
  336.  
  337.  
  338.              9)   Totally   invisible  and   incremental   garbage 
  339.              collection.   There   is  NEVER  any  wait  for  this 
  340.              function.
  341.  
  342.  
  343.              10)  A  tree  structured,   dynamically  configurable 
  344.              lexical scoping system. The work of many  programmers 
  345.              can  be coupled together in the form of libraries and 
  346.              nested domains. 
  347.  
  348.              Each lexically scoped domain is a hidden space  which 
  349.              communicates  with the parent domain via exports  and 
  350.              imports. Domains can be linked together under program 
  351.              control to achieve any desired configuration.
  352.  
  353.  
  354.              11) The Grammar Rule Notation is an integral feature.
  355.  
  356.  
  357.              12) Keyword redefinition makes porting code easy.
  358.  
  359.  
  360.         The  cost  of this system is $200 for the MSDOS version.
  361.         The corresponding Runtime System is available for $200.00
  362.  
  363.  
  364.         .VMA PROLOG Large model Virtual Memory System
  365.  
  366.         This  system  has  additional forms of  virtual  memory.  It  was 
  367.         intended  for  deep  reasoning  problems.  Contact  us  for  more 
  368.         information.
  369.  
  370.              VMA PROLOG Development System:  $250.00
  371.              VMA PROLOG Runtime System:      $250.00
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.                                         6
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                                  Upgrade Policy
  401.  
  402.              Half the cost of any A.D.A. PROLOG system may be credited to 
  403.         the purchase of a higher level version.      The full cost of VMS 
  404.         prolog  may  be  applied to the purchase of VMI  or  VML  PROLOG. 
  405.         Updates to a particular level product vary from $15.00 to $35.00.
  406.  
  407.  
  408.                               Technical Information
  409.  
  410.              Technical information may be obtained at (215) - 646- 4894
  411.  
  412.              Perhaps we can answer the following questions in advance:
  413.  
  414.              There is no support for:  APPLE II, Atari, Commodore, or  
  415.         CPM 80 .  Other machines from these manufactures may be supported 
  416.         in the future.
  417.  
  418.              The MSDOS products are available on 5" and 8" diskettes.
  419.  
  420.  
  421.  
  422.                               To Place Your Order:
  423.  
  424.              You may place your order at the following number:
  425.  
  426.                         (215)-646-4894   - day and night.
  427.  
  428.  
  429.  
  430.                                      Returns
  431.  
  432.              The  software may be returned within 30 days of purchase for 
  433.         a full refund.  This applies whether or not the software has been 
  434.         used.  We do ask that manuals, disks and packaging be returned in 
  435.         excellent condition.
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.                                         7
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                                   Installation
  467.  
  468.              You will need an IBM PC compatible machine with a minimum of 
  469.         256  kbytes  of  memory.  ED PROLOG benefits from  all  available 
  470.         memory up to the 1 megabyte INTEL limit.
  471.  
  472.              To  determine the amount of TPA your machine  has,  run  the 
  473.         "chkdsk" program which is supplied with DOS. The last line reads:
  474.  
  475.                                 XXXXXX bytes free
  476.  
  477.         where XXXXXX is a six digit number.
  478.  
  479.         If  this  number  is greater than 200000,  ED  PROLOG  will  have 
  480.         reduced workspace.  If it's over 245000,  the amount of memory is 
  481.         optimal. If this is not the case, there are two possibilities:
  482.  
  483.         1) The machine doesn't have enough memory.
  484.  
  485.         2)  Something  else is removing memory from TPA,  such as  a  co-
  486.         resident  program,  a  ramdisk,  a large dos driver,  or a  large 
  487.         number of file or disk buffers.
  488.  
  489.         If  you're  short of memory,  make sure that no  other  programs, 
  490.         ramdisks,  or drivers besides DOS are running in the machine. You 
  491.         may find it helps to eliminate (by renaming) the config.sys  file 
  492.         when you intend to run ED PROLOG.
  493.  
  494.         In order to use the built-in predicate "exec", the system must be 
  495.         configured for memory, which is detailed further on.
  496.  
  497.  
  498.                       How to run the Demonstration Programs 
  499.                         without Knowing What You're Doing
  500.  
  501.              We strongly advise that you purchase the book Programming in 
  502.         PROLOG by Clocksin and Mellish,  publisher Springer Verlag, 1981. 
  503.         For  the  impatient we give some advice.  Type the  demonstration 
  504.         program you wish to run.  There must be at least one entry  point 
  505.         within  the  program.  
  506.  
  507.         Note:  Please  understand that these are demonstrations programs. 
  508.         Regarding  user  interface,  they are poorly  written.  You  will 
  509.         probably have to read Clocksin and Mellish to appreciate that the 
  510.         following examples of input are not equivalent: "yes." , "yes" .
  511.  
  512.  
  513.  
  514.         The Hematology Diagnosis Program - "hemat"
  515.  
  516.              Although  the  logical structure is not as sophisticated  as 
  517.         that of "animals", it is interesting for several reasons:
  518.  
  519.              1)  The  program  evaluates numerical data to  arrive  at  a 
  520.         diagnosis.
  521.  
  522.  
  523.                                         8
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.              2) Although inaccurate, it demonstrates that useful question 
  534.         answering systems are not difficult to write in PROLOG.
  535.  
  536.              3)  There  are  some mistakes in  the  program,  which  only 
  537.         slightly impede its usefulness. 
  538.  
  539.              This  program  uses  structure  input.  Terminate  all  your 
  540.         answers with a period, as in "y.<CR>", or "no.<CR>".
  541.         The starting point is "signs.<CR>".  PROLOG will prompt  you 
  542.         for  signs  of  anemia.  The  program attempts  to  diagnose  two 
  543.         varieties of a hemolytic anemia.
  544.              The program could use a good working over by a  hematologist 
  545.         and we would be delighted to collaborate.
  546.  
  547.  
  548.         Prime Number Generator - "sieve"
  549.  
  550.              This  program demonstrates that anything can be programed in 
  551.         PROLOG if one tries hard enough.  Asking the  question   
  552.           "primes( 50, L ).<CR>" causes a list of prime numbers less than 
  553.         50  to be printed out.  "Sieve" is heavily recursive and  quickly 
  554.         exhausts the stack space of the small model interpreters.
  555.  
  556.  
  557.         Grrules
  558.  
  559.              This  is  an  example  of the use  of  the  definite  clause 
  560.         grammer  notation.  PD  PROLOG  does not  have  this  translation 
  561.         facility,  but ED PROLOG and  all of our other versions do. It is 
  562.         possible  to  perform  the  translations  by  hand  if  you  have 
  563.         thoroughly  read  C  & M.  Then you would have  the  pleasure  of 
  564.         asking:
  565.  
  566.                  ?-sentence( X, [every,man,loves,a,woman], [] ).
  567.  
  568.         and  having the meaning elucidated as a statment in the predicate 
  569.         calculus.
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.                                         9
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                                Special Offer  # 1
  599.  
  600.              For  some  inexplicable reason,  demonstration programs  are 
  601.         hard to come by. We are too busy writing PROLOG fill this gap. We 
  602.         will  reward the contribution of "cute" sample programs with  the 
  603.         following:
  604.  
  605.              1) A free  copy of type VMI virtual memory PROLOG
  606.  
  607.              2) The sample program will be published as an intact file 
  608.              together with whatever comments or advertisments the author
  609.              may see fit to include, on our distribution disks. 
  610.  
  611.              3) Exceptional contributions may merit a copy of type VML 
  612.              large  model virtual memory PROLOG which now incorporates  a 
  613.              UNIX1 style tree structured domain system.
  614.  
  615.  
  616.                                 Special Offer # 2
  617.  
  618.  
  619.              If  you are a hardware manufacturer and would like a  PROLOG 
  620.         language  for your system,  the solution is simple.  Just send us 
  621.         one  of  your machines!  Provided your system  implements  a  "C" 
  622.         compiler, it will be ported in no time flat.
  623.  
  624.  
  625.         ______
  626.              1. Trademark of AT & T.
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.                                        10
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                          Writing Programs For ED PROLOG
  665.  
  666.              You do not type in programs at the "?-" prompt.  There is no 
  667.         built-in  editor.  The command "consult( user )" is accepted  but 
  668.         does not cause PROLOG to enter an editing mode.  We feel that the 
  669.         most universally acceptable editing method is for the user to use 
  670.         a text editor of choice,  which can be invoked from within PROLOG 
  671.         by the "exec" predicate.
  672.  
  673.              Use  Wordstar  or your customary editor to write a  program. 
  674.         Then  run  PD  PROLOG and use the consult function  to  load  the 
  675.         program.
  676.  
  677.              In  all  cases except PD PROLOG,  you can  run  your  editor 
  678.         without leaving PROLOG by use of the "exec" predicate.
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.                                        11
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                              Running the Interpreter
  731.  
  732.         COMMANDS: Give commands in lower case.
  733.  
  734.         TO RUN:
  735.              Invoke PROLOG.EXE. After the "?-" prompt appears,
  736.                type "consult( <filename><CR> )", where <filename> is the 
  737.                desired database.
  738.              To exit, type "exitsys.<CR>"
  739.  
  740.         TO ENTER A FACT:
  741.              Don't do it except with the "assert" predicates. This is the 
  742.              most  frequently misunderstood aspect of A.D.A.  Prolog.  If 
  743.              you want to enter a bunch of facts,  put them in a file  and 
  744.              "consult" them using the "consult" predicate.
  745.  
  746.         TO ASK A QUESTION:
  747.              At the prompt, type "<expression>.<CR>", where 
  748.              <expression>  is  a  question as described by  Clocksin  and 
  749.              Mellish. Be sure to terminate the question with a period.
  750.              The question may be up to 500 characters long.
  751.  
  752.         TO INPUT A STRUCTURE AT THE KEYBOARD:
  753.              The structure may be up to 500 characters in length. Be sure 
  754.              to terminate with a period.
  755.  
  756.         TO ASK FOR ANOTHER SOLUTION:
  757.              If a solution has been provided, the PROLOG interpreter will 
  758.              ask  "More?  (Y/N):".  Only  if  a "y"  is  typed  will  the 
  759.              interpreter perform a search.
  760.  
  761.         TO ABORT A SEARCH:
  762.              Simply   type   the  escape  key.   The   interpreter   will      
  763.              respond  with  "Interrrupted.",  and return to  the  command      
  764.              prompt.
  765.  
  766.         TO LOAD ANOTHER DATABASE:
  767.              Type  "consult(<filename>).<CR>" The file name must have the 
  768.              extension  ".PRO".  It  is  not  necessary  to  include  the 
  769.              extension in the argument of consult. The file name as given 
  770.              must not be the same as a predicate name in the file or  any 
  771.              file which will be loaded.
  772.  
  773.         TO TRACE:
  774.              When the system displays the prompt "?-", type "trace.<CR>".  
  775.              The display will likely move too rapidly for you to read. To 
  776.              stop the display,  type Control S.  To restart the  display, 
  777.              type  Control S.  To turn the trace      display  off,  type 
  778.              "notrace<CR>"  at  the  prompt  "?-".   The  interrupt  menu 
  779.              contains  additional  options,  such  as sending  all  trace 
  780.              output to a file, as well as display at the console.
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.                                        12
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.         TO INTERRUPT A PROGRAM:
  797.              See  the  section  entitled  "The  Interrupt  Menu"  for   a 
  798.              description  of the flexible options.  Basically,  one types 
  799.              ESC  to terminate a program,  while Control V or  Control  I 
  800.              interrupt a program. 
  801.  
  802.         TO RECONSULT A FILE:
  803.              The   predicate  "recon"  is  identical  to  the   Edinburgh 
  804.              predicate "reconsult."
  805.  
  806.         TO REMOVE A DATABASE:
  807.              Type "forget(<filename>).<CR>"
  808.  
  809.         TO EDIT A FILE:
  810.  
  811.              The  "exec"  function allows you to execute  other  programs 
  812.         from within PROLOG. In the type PD, it is restricted to a program 
  813.         of  name "PROLOGED",  which is a text editor supplied by you,  or 
  814.         the  program  "PROLOGED.COM"  supplied by  us.  This  is  covered 
  815.         further on.
  816.  
  817.  
  818.         TO EXIT TO THE OPERATING SYSTEM:
  819.              Type "exitsys.<CR>"
  820.  
  821.              The system is totally interactive; any commands the operator 
  822.         gives  are and must be valid program statements.  Statements must 
  823.         terminate with a period.      All commands which take a file name 
  824.         also  accept a path name.  Any name which is not a  valid  PROLOG 
  825.         atom (refer to C & M) must be enclosed in single quotes. Thus one 
  826.         could say
  827.  
  828.              consult( expert )
  829.  
  830.         but one would need single quotes with
  831.  
  832.              consult( 'b:\samples\subtype\expert' ).
  833.  
  834.  
  835.         To exit the system, type "exitsys.<CR>"
  836.  
  837.         Atoms  may contain MSDOS pathnames if they are enclosed by single 
  838.         quotes, ie.,  '\b:\samples\animal' .
  839.  
  840.         You may consult more than one file at a time.  However, all names 
  841.         are public and name conflicts must be avoided. The order in which 
  842.         modules are loaded may,  in cases of poor program design,  affect 
  843.         the behavior.
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.                                        13
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.                              Command Line Arguments
  863.  
  864.              ED and PD PROLOG accept one command line argument,  which is 
  865.         the name of a "stream" which replaces the console for input.  The 
  866.         "stream"  in  MSDOS is a pipe or file which supplies input  until 
  867.         end-of-file is reached. Control then reverts back to the console. 
  868.         To avoid noisy parser error messages when end-of-file is reached, 
  869.         the  last command in the file should be "see( user )." See  Simon 
  870.         Blackwell's PIE program for an example of this.
  871.  
  872.  
  873.  
  874.                                A Reference of Note
  875.  
  876.  
  877.              With  minor  exceptions,  the syntax is a superset  of  that 
  878.         described  by  Clocksin and Mellish in the  book  Programming  in 
  879.         Prolog by W.F.  Clocksin and C.S.  Mellish, published by Springer 
  880.         Verlag in Berlin, Heidelberg, New York, and Tokyo. We shall refer 
  881.         to this book as "C & M".
  882.  
  883.  
  884.              There   are  very  few   syntactical   differences,   mostly 
  885.         unrecognized and/or minor.
  886.              When  an operator is declared using the "op" statement,  the 
  887.         operator must be enclosed in single quotes in the "op"  statement 
  888.         itself,  if  it would not otherwise be a legal Edinburgh functor. 
  889.         Subsequently,  however,  the parser will recognize it for what it 
  890.         is,  except  in  the "unop" statement,  where it  must  again  be 
  891.         enclosed in single quotes.
  892.  
  893.              Variable numbers of functor paramaters are supported.
  894.  
  895.              A  goal  may be represented by a  variable,  which  is  less 
  896.         restrictive  than  the  C  &  M requirement  that  all  goals  be 
  897.         functors.  The  variable must be instantiated to a  functor  when 
  898.         that goal is pursued.
  899.  
  900.              Rules which appear inside other expressions must be enclosed 
  901.         in  parenthesis  if  the "," operator is to be  recognized  as  a 
  902.         logical connective.
  903.  
  904.              All  infix  operators described by C & M,  and user  defined 
  905.         infix,  prefix, and postfix operators with variable associativity 
  906.         and precedence are supported exactly as in C & M.
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.                                        14
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                   Editing and Preparing Programs for ADA PROLOG
  929.  
  930.              ADA  PROLOG programs are ordinary text files.  They  may  be 
  931.         prepared using your customary text editor, in which case one then 
  932.         runs  PROLOG.EXE,  "consults" a file which must have an extension 
  933.         of ".PRO", and enters the interactive, goal giving phase.
  934.  
  935.              The alternative to this is to prepare files without  leaving 
  936.         PROLOG, which requires that more memory be available. We supply a 
  937.         small  text editor on disk,  "PROLOGED.COM".  This,  or any other 
  938.         editor  which is renamed as such,  can be used provided that  one 
  939.         realizes  that whatever memory the editor uses is  not  available 
  940.         for use by PROLOG.  The built in predicate "exec" is used to load 
  941.         an editor in PD PROLOG, or any program in ED PROLOG.
  942.  
  943.              In  order  to use the "exec" function,  it is  necessary  to 
  944.         reserve  enough space using the program "PRCONF.EXE" to configure 
  945.         the  PROLOG  system.  This  procedure is described  in  the  next 
  946.         section. Once this is done, the syntax is as follows:
  947.  
  948.         exec( <string1>, <string2>, <string3>,... )
  949.  
  950.              Execute  a  DOS  program  from  within  PROLOG.   The   file 
  951.         "COMMAND.COM" must be in the DOS search path.  A <string> may not 
  952.         contain  embedded blanks.  Acceptable forms:
  953.  
  954.              exec( ws, 'test.pro' )
  955.              exec( 'ws test.pro' )
  956.              exec( prologed )
  957.  
  958.              Interestingly the form
  959.  
  960.              exec( command )
  961.  
  962.         causes an apparent exit to DOS. However, you can resume PROLOG by 
  963.         typing "exit", <CR>, and all your work will be intact.
  964.  
  965.  
  966.              In the type PD,  the capabilities of the "exec" function are 
  967.         limited. The only form is:
  968.  
  969.              exec
  970.  
  971.         with  no  arguments,   which  causes  execution  of  the  program 
  972.         "PROLOGED.COM",  or "PROLOGED.EXE".  We supply such a program  on 
  973.         disk, and it is a small editor.
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.                                        15
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                     Configuring the PROLOG System for Memory
  995.  
  996.  
  997.              The PRCONF.EXE program,  which can be used with versions 1.9 
  998.         and higher, allows you to configure the memory used by the PROLOG 
  999.         system.   Thus  the  amount of  memory left over for use  by  the 
  1000.         "exec"  function  can be controlled.   Type PD does not  have  an 
  1001.         "exec"  function,  and therefore has no need of this program.  It 
  1002.         grabs all of transient program area by default.
  1003.  
  1004.              To  use PRCONF,  copy your ??PROLOG.EXE file to a file named 
  1005.         PROLOG.EXE.  PRCONF looks for this file, queries the operator for 
  1006.         the amount of workspace desired,  and then alters the  file.  The 
  1007.         total  space  used by the altered copy is the sum of the size  of 
  1008.         PROLOG.EXE itself plus the size of the workspace you specified.
  1009.  
  1010.              After  you've  done  this,  run PROLOG.EXE and look  at  the 
  1011.         copyright message.  It will display the highest address used. Use 
  1012.         the  "exec"  function  to  load  and  execute  a  program  you're 
  1013.         interested  in having available without leaving  PROLOG.  If  the 
  1014.         "exec"  function  fails,  you  must rerun PRCONF and  reduce  the 
  1015.         amount  of workspace,  thereby leaving more room for  the  "exec" 
  1016.         function.
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.                                        16
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                          The Built In Predicate Library
  1061.  
  1062.                         Available Operators in PD and ED PROLOG
  1063.  
  1064.  
  1065.         Column 1 gives the function symbol. 
  1066.  
  1067.         Column 2 gives the precedence. The range of precedence is 1 to 255.
  1068.         A zero in the precedence column indicates the symbol is parsed as 
  1069.         a functor, and precedence is meaningless in this case.
  1070.  
  1071.         Column 3 gives the associativity. 
  1072.         A zero in the associativity column indicates the symbol is parsed
  1073.         as a functor, and associativity is meaningless in this case.
  1074.  
  1075.         Column 4 indicates which version the function is available in. 
  1076.         Unless otherwise noted, the function is available in all versions.
  1077.         Nonstandard predicates are indicated by "NS".
  1078.  
  1079.  
  1080.         op/pred      precedence    associativity         availability
  1081.  
  1082.         "!"             0             0
  1083.         "|"             0             0
  1084.         "="             40,           XFX                   
  1085.         "=="            40,           XFX                   
  1086.         "\\="           40,           XFX                   
  1087.         "\\=="          40,           XFX       
  1088.         "/"             21,           YFX       
  1089.         "@="            40,           XFX       
  1090.         ">="            40,           XFX       
  1091.         "=<"            40,           XFX       
  1092.         ">"             40,           XFX       
  1093.         "<"             40,           XFX       
  1094.         "-"             31,           YFX        
  1095.         "*"             21,           YFX
  1096.         "+"             31,           YFX
  1097.         "=.."           40,           XFX
  1098.         "-->"           255,          YFY       (not  in  PD PROLOG)
  1099.         "?-"            255,          FY  
  1100.               
  1101.  
  1102.  
  1103.         "arg"           0,            0,                                   
  1104.         "asserta"       0,            0,                                   
  1105.         "assertz"       0,            0,                                   
  1106.         "atom"          0,            0,                                   
  1107.         "atomic"        0,            0,                                   
  1108.         "batch"         0,            0
  1109.         "clause"        0,            0,                                   
  1110.         "clearops"      0,            0,                                   
  1111.         "cls"           0,            0,       NS
  1112.         "concat"        0,            0,                                   
  1113.         "consult"       8,            FX,                                  
  1114.         "crtgmode"      0,            0,       NS
  1115.  
  1116.  
  1117.                                        17
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.         "crtset"        0,            0,       NS
  1127.         "curset"        0,            0,       NS
  1128.         "curwh"         0,            0,       NS
  1129.         "debugging      0,            0,                                   
  1130.         "dir"           0,            0,                                   
  1131.         "display"       0,            0,                                   
  1132.         "dotcolor"      0,            0,       NS
  1133.         "drawchar"      0,            0,       NS
  1134.         "drawdot"       0,            0,       NS
  1135.         "drawline"      0,            0,       NS
  1136.         "exec"          0,            0,                                   
  1137.         "exitsys"       0,            0,       NS                          
  1138.         "forget"        0,            0,       NS                          
  1139.         "functor"       0,            0,                                   
  1140.         "get0"          8,            FX,                                  
  1141.         "integer"       0,            0,                                   
  1142.         "is"            40,           XFX,                                 
  1143.         "listing"       0,            0,                                   
  1144.         "memleft"       0,            0,      NS
  1145.         "mod"           11,           XFX,                                 
  1146.         "name"          0,            0,                                   
  1147.         "nl"            0,            0,                                   
  1148.         "nodebug"       0,            0,       (not in PD PROLOG)
  1149.         "nonvar"        0,            0,                                   
  1150.         "nospy"         50,           FX,      (not in PD PROLOG)
  1151.         "not"           60            FX                                   
  1152.         "notrace"       0,            0,       (not in PD PROLOG)
  1153.         "op"            0,            0,                                   
  1154.         "popoff"        0,            0,       NS
  1155.         "popoffd"       0,            0,       NS
  1156.         "popon"         0,            0,       NS
  1157.         "popond"        0,            0,       NS
  1158.         "print"         0,            0,                                   
  1159.         "prtscr"        0,            0,       NS
  1160.         "put"           0,            0,                                   
  1161.         "ratom"         0,            0,                                   
  1162.         "read"          0,            0,                                   
  1163.         "recon"         0,            0, (Note: this is "reconsult")
  1164.         "repeat"        0,            0,                                   
  1165.         "retract"       0,            0                                    
  1166.         "rnum"          0,            0,                                   
  1167.         "see"           0,            0,       
  1168.         "seeing"        0,            0,       
  1169.         "seen"          0,            0,       
  1170.         "skip"          0,            0,       (not in PD PROLOG)
  1171.         "spy"           50,           FX,      
  1172.         "tab"           0,            0,                                   
  1173.         "tell"          0,            0,       
  1174.         "telling"       0,            0,       
  1175.         "told"          0,            0,       
  1176.         "trace"         0,            0,       (not in PD PROLOG)
  1177.         "true"          0,            0,                                   
  1178.         "unop"          0,            0,                                   
  1179.         "var"           0,            0,                                   
  1180.         "write"         0,            0,                                   
  1181.  
  1182.  
  1183.                                        18
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                         Description of the Modifications.
  1193.              
  1194.  
  1195.         I/O Redirection
  1196.  
  1197.         I/O  redirection is a feature described by Clocksin and  Mellish. 
  1198.         The predicates "see",  "seeing",  "seen",  "tell", "telling", and 
  1199.         "told" are used to select the streams used for input and output. 
  1200.  
  1201.              The  predicates  "seen" and "told" require as arguments  the 
  1202.         name of the stream that is to be closed.  This enables the system 
  1203.         to  remember the indices of several streams and switch  back  and 
  1204.         forth between them.
  1205.  
  1206.              The  predicate "batch",  when inserted at the beginning of a 
  1207.         stream file, has the following properties:
  1208.  
  1209.              1)  The normal prompt,  "?-",  and advisory messages do  not 
  1210.              appear at the screen.
  1211.  
  1212.              2)  It is self cancelling if the input stream is  reassigned 
  1213.              to the console.
  1214.  
  1215.              3) It may also be cancelled by the predicate "batch".
  1216.  
  1217.  
  1218.  
  1219.         call( <goal> )
  1220.  
  1221.              The  predicate as defined in C & M is obsolete.  The purpose 
  1222.         was  to permit a goal search where the goal name was  a  variable 
  1223.         instantiated  to  some functor name.  A.D.A.  permits writing  of 
  1224.         goals with such names,  so the mediation of the "call" clause  is 
  1225.         no longer necessary.
  1226.  
  1227.              The  "call"  predicate  may  be  trivially  implemented  for 
  1228.         compatibility with the PROLOG definition
  1229.  
  1230.                                  call( X ) :- X.
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.                                        19
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.         debugging
  1259.  
  1260.              "Debugging"  prints a list of the current  spypoints.  After 
  1261.         each name a sequence of numbers may appear, indicating the number 
  1262.         of  arguments that is a condition of the trace.  The  word  "all" 
  1263.         appears  if  the  number of arguments is not a condition  of  the 
  1264.         trace.
  1265.  
  1266.         op( <prec>, <assoc>, <functor> )
  1267.  
  1268.              Defines  the  user  definable  grammar  of  a  functor.  The 
  1269.         definition conforms to that in C & M. We mention here a minor but 
  1270.         important point.  If <functor> is not a valid PROLOG atom it must 
  1271.         be   enclosed  in  single  quotes  when  declared  in  the   "op" 
  1272.         declaration.  It  is  not necessary or legal to do this when  the 
  1273.         functor is actually being used as an operator.  In version 1.6, a 
  1274.         declared  or built-in operator can be used either as an  operator 
  1275.         or as a functor. For example, 
  1276.  
  1277.                                  +(2,3) = 2 + 3.
  1278.  
  1279.         is a true statement.
  1280.  
  1281.              Declared  operators  are annotated in the directory  display 
  1282.         with their precedence and associativity.
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.                                        20
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.         Output predicates
  1325.  
  1326.            display
  1327.            write
  1328.            print
  1329.            put
  1330.  
  1331.              These  functions  have  been  modified  to  accept  multiple 
  1332.         arguments in the form:
  1333.  
  1334.                        print( <arg1>, <arg2>, <arg3>,... )
  1335.  
  1336.         Thus, "put( a, b, c )" would result in the display of "abc".
  1337.              The  names  of  some  PROLOG atoms that may  occur  are  not 
  1338.         accepted  by  the  PROLOG scanner  unless  surrounded  by  single 
  1339.         quotes.  This only applies when such an atom is read in, not when 
  1340.         it is internally generated. Nevertheless, this presents us with a 
  1341.         problem:  We  would  like to be capable of writing  valid  PROLOG 
  1342.         terms to a file. In some cases, it is necessary to add the single 
  1343.         quotes.  In other cases,  such as human oriented output, they are 
  1344.         an irritant. The modified definitions of the following predicates 
  1345.         are an attempt at a solution:
  1346.  
  1347.              display
  1348.              Operator  precedence  is ignored,  all functors are  printed 
  1349.              prefix and single quotes are printed if needed or they  were 
  1350.              supplied if and when the atom was originally input.
  1351.  
  1352.              write
  1353.              Operator  precedence is taken into account and operators are 
  1354.              printed according to precedence.  Single quotes are  printed 
  1355.              under the same conditions as for "display."
  1356.  
  1357.              print
  1358.              Operator  precedence is taken into account and operators are 
  1359.              printed  according to precedence.  Single quotes  are  never 
  1360.              displayed.  This  is  a  human oriented form of  output  and 
  1361.              should  never  be used for writing of terms for  the  PROLOG 
  1362.              scanner.
  1363.  
  1364.  
  1365.  
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.                                        21
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.         get0
  1391.         read
  1392.  
  1393.              The  functions  "get0"  and "read"  have  been  extended  to 
  1394.         support input from a stream other than the one currently selected 
  1395.         by "see". To direct output to a file or other stream, an optional 
  1396.         argument  is used.  For example,  "get0( char,  <file name> )" or 
  1397.         "get0( char,  user )" would cause input to come from <file  name> 
  1398.         or the console.  If the file has not already been opened,  "get0" 
  1399.         will fail.
  1400.  
  1401.         reconsult( <file> )
  1402.  
  1403.         The name of this predicate has been changed to "recon". Otherwise 
  1404.         it is identical in function to the C & M version.
  1405.  
  1406.  
  1407.         see( <stream> )
  1408.              
  1409.              This  predicate  does  not cause the previous stream  to  be 
  1410.         rewound  or  closed.  If the argument of "see"  is  a  previously 
  1411.         "seen" stream,  reading resumes at the place in the file where it 
  1412.         was interrupted.
  1413.  
  1414.         seen( <stream> )
  1415.  
  1416.              According  to the C & M standard,  "seen" closes the  stream 
  1417.         which  the  system is "seeing".  In this system,  many  different 
  1418.         streams  can be open at the same time.  "seen" reduces  the  open 
  1419.         file overhead by closing a stream which is no longer needed.
  1420.  
  1421.         tell( <stream> )
  1422.              
  1423.              This  predicate  does  not cause the previous stream  to  be 
  1424.         rewound  or  closed.  If the argument of "tell" is  a  previously 
  1425.         "told" stream,  writing resumes at the place in the file where it 
  1426.         was interrupted.
  1427.  
  1428.         told( <stream> )
  1429.  
  1430.              According  to the C & M standard,  "told" closes the  stream 
  1431.         which  the system is "telling".  In this system,  many  different 
  1432.         streams  can  be open at the same time.  "told" reduces the  open 
  1433.         file overhead by closing a stream which is no longer needed.
  1434.  
  1435.  
  1436.  
  1437.  
  1438.              Atoms enclosed by single quotest, eg. '\nthis is a new line' 
  1439.         can contain the escape sequences
  1440.  
  1441.              '\n', '\r', '\t' and '\''.
  1442.  
  1443.              If these atoms are printed by "display" or "write" they  are 
  1444.         printed  just  as they are.  If they are printed by  the  "print" 
  1445.  
  1446.  
  1447.                                        22
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.         clause they are translated as follows:
  1457.  
  1458.         '\n'  results  in  the printing of a carriage return and  a  line 
  1459.         feed.
  1460.         '\r' results in the printing of a carriage return only.
  1461.         '\t' results in the printing of a tab character.
  1462.         '\'' allows the printing of a single quote within a quoted atom.
  1463.  
  1464.              The "portray" feature is not presently implemented.
  1465.  
  1466.  
  1467.  
  1468.  
  1469.  
  1470.  
  1471.  
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501.  
  1502.  
  1503.  
  1504.  
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.                                        23
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.                         Description of the New Predicates
  1523.  
  1524.  
  1525.         clearops-
  1526.  
  1527.              Nullify  the  operator  status  of  every  operator  in  the 
  1528.              database.
  1529.  
  1530.         concat( (<variable> | <functor>), <List> )
  1531.  
  1532.              A  list  of functors or operators is concatenated  into  one 
  1533.         string,  which becomes the name of a new atom to which <variable> 
  1534.         or <functor> must match or be instantiated.
  1535.  
  1536.  
  1537.         dir( option )
  1538.  
  1539.              Provide  an  alphabetized listing to the console  of  atoms, 
  1540.         constants,   or   open  files.   Without  options,   simply  type 
  1541.         "dir.<CR>". Options are:
  1542.  
  1543.              dir( p ) - list clause names only.
  1544.              dir( c ) - list consulted files only.
  1545.  
  1546.         Consulted files are prefixed by "S:".
  1547.  
  1548.  
  1549.         exitsys
  1550.  
  1551.              Exit to the operating system.
  1552.  
  1553.  
  1554.  
  1555.         forget( <file name> )
  1556.              Make  a database unavailable for use and reclaim the storage 
  1557.         it occupied.
  1558.  
  1559.  
  1560.  
  1561.         ratom( <arg>, <stream> )-
  1562.              Read an atom from the input stream,  to which <arg>  matches 
  1563.         or  is  instantiated.  <stream> is optional.  If <stream> is  not 
  1564.         given, the input stream defaults to the standar input.
  1565.              Input is terminated by a CR or LF, which are not included in 
  1566.         the stream.
  1567.  
  1568.  
  1569.  
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575.  
  1576.  
  1577.  
  1578.  
  1579.                                        24
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.                              Arithmetic Capabilities
  1589.  
  1590.              Integer  arithmetic is supported.  Numbers are 32 bit signed 
  1591.         quantities. The following arithmetic operators are supported:
  1592.  
  1593.                       "+", "-", "*", "/", <, <=, >, >=, mod.
  1594.  
  1595.         Arithmetic operators must never be used as goals,  although  they 
  1596.         may be part of structures. It is legal to write:
  1597.  
  1598.                                     X = a + b
  1599.              
  1600.         which  results in the instantiation of X to the struture (a + b). 
  1601.         But the following is not legal:
  1602.  
  1603.                        alpha( X, Y ) :- X + Y, beta( Y ).
  1604.  
  1605.  
  1606.         Evaluation  of  an arithemtic expression is mediated by the  "is" 
  1607.         and inequality predicates.  For instance,  the following would be 
  1608.         correct:
  1609.  
  1610.                          alpha( X, Y, Z ) :- Z is X + Y.
  1611.  
  1612.                          beta( X, Y ) :- X + 2 < Y + 3.
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.                                        25
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.                             Memory Metric Predicate
  1655.  
  1656.              The purpose of this predicate is to give the prolog system 
  1657.         a  sense  of  how much memory remains so  that  expensive  search 
  1658.         strategies  can  be  controlled.  It is not possible  to  exactly 
  1659.         quantify how much memory remains.  At the lowest level, there are 
  1660.         two  types of memory - the stack and the heap.  The stack expands 
  1661.         down  from  high  memory,  while  the heap  tends  to  expand  at 
  1662.         unpredictable intervals upwards.  If the stack and heap meet, the 
  1663.         prolog  system  must abort the search and return to  the  prompt. 
  1664.         Judicious  use  of  the  memory  metric  predicates  reduces  the 
  1665.         probability of this happening.
  1666.  
  1667.              The  stack is easy to quantify because it expands  downwards 
  1668.         in  a  predictable  way with recursion.  The symbol  space  is  a 
  1669.         "heap".  For  those  interested,  the structure of  the  heap  is 
  1670.         determined  by  the C compiler under which Prolog  was  compiled. 
  1671.         There  is  a function internal to Prolog known as  the  allocator 
  1672.         searches  the  heap for enough contiguous memory to create a  new 
  1673.         symbol.  The  heap resembles a piece of Swiss cheese;  the  holes 
  1674.         represent symbols and already allocated memory while the remained 
  1675.         is  searched  by the allocator for a piece of  contiguous  memory 
  1676.         large enough to satisfy a request.  If one cannot be  found,  the 
  1677.         uppermost  bound of the heap is expanded upwards,  and that bound 
  1678.         is  the  number  which we measure for an  estimate  of  remaining 
  1679.         memory.
  1680.  
  1681.              The  sqace between the top of the heap,  and  the top of the 
  1682.         stack,  which we call "gap",  serves as a rough guide to how much 
  1683.         memory  remains.  The  demands  of the system  are  not  entirely 
  1684.         predictable,  however.  For example, the creation of a new symbol 
  1685.         larger  than "gap" would cause an abort.  The user must  use  the 
  1686.         numbers  supplied  by  these  functions  as  a  heuristic  guide, 
  1687.         tempered  by  experience,  to  minimize  the  possibility  of  an 
  1688.         unexpected abort.
  1689.  
  1690.         "Gap" is measured in 256 byte pages.
  1691.  
  1692.         memleft( X )
  1693.  
  1694.         If  the argument is an integer,  this is satisfied if the size of 
  1695.         "gap" is greater than "X".
  1696.  
  1697.         If the argument is a variable,  it is instantiated to the  amount 
  1698.         of "gap" remaining.
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.                                        26
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.                          IBM PC Video Display Predicates
  1721.  
  1722.  
  1723.         A high level method is provided for drawing and displaying on the 
  1724.         screen of IBM PC and compatible computers. 
  1725.  
  1726.  
  1727.  
  1728.         cls
  1729.  
  1730.         Clear  the screen and position the cursor at the upper left  hand 
  1731.         corner.
  1732.  
  1733.  
  1734.  
  1735.         crtgmode( X )
  1736.  
  1737.         Matches  the  argument to the mode byte of the display  which  is 
  1738.         defined as follows:
  1739.  
  1740.                   mode           meaning
  1741.  
  1742.                    0             40 x  25  BW (default)
  1743.                    1             40 x  25  COLOR
  1744.                    2             80 x  25  BW
  1745.                    3             80 x  25  COLOR
  1746.                    4            320 x 200  COLOR
  1747.                    5            320 x 200  BW
  1748.                    6            640 x 200  BW
  1749.                    7             80 x  25  monochrome  display card
  1750.  
  1751.  
  1752.  
  1753.         crtset( X )
  1754.          
  1755.         This  sets the mode of the display.  The argument must be one  of 
  1756.         the modes given above. 
  1757.  
  1758.  
  1759.  
  1760.         curset( <row>, <column>, <page> )
  1761.  
  1762.         Sets the cursor to the given row, column, and page. The arguments 
  1763.         must be integers.
  1764.  
  1765.  
  1766.  
  1767.         curwh(  <row>, <column> )
  1768.  
  1769.         Reports the current position of the cursor.  The argument must be 
  1770.         an integer or variable. The format is:
  1771.  
  1772.              1) page zero is assumed.
  1773.              2) The row is in the range 0 to 79, left to right.
  1774.              3) The column is in the range 0 to 24, bottom to top.
  1775.  
  1776.  
  1777.                                        27
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.         dotcolor( <row>, <column>, <color> )
  1787.  
  1788.         The  argument  <color> is matched to the color of  the  specified 
  1789.         dot. The monitor must be in graphics mode.
  1790.  
  1791.  
  1792.  
  1793.         drawchar( <character>, <attribute> )
  1794.  
  1795.         Put a character at the current cursor position with the specified 
  1796.         attribute.  The  arguments  <character> and <attribute>  must  be 
  1797.         integers.  Consult  the IBM technical reference manual  regarding 
  1798.         attributes.
  1799.  
  1800.  
  1801.  
  1802.         drawdot( <row>, <column>, <color> )
  1803.  
  1804.         Put  a dot at the specified position.  The monitor must be in the 
  1805.         graphics  mode.  The  arguments  must be  integer.  The  argument 
  1806.         <color> is mapped to integers by default in the following manner:
  1807.  
  1808.  
  1809.  
  1810.         drawline( <X1>, <Y1>, <X2>, <Y2>, <color> )
  1811.  
  1812.         Draw a line on the between the coordinate pairs. The monitor must 
  1813.         be in the graphics mode and the arguments are integer.
  1814.  
  1815.  
  1816.  
  1817.         prtscr
  1818.  
  1819.         Print  the  screen  as it currently appears.  Be  sure  that  the 
  1820.         printer  is  on  line and ready before invoking  this  predicate, 
  1821.         since otherwise, the system may lock up or abort.
  1822.  
  1823.  
  1824.  
  1825.         The  integer argument <color> referred to in the above predicates 
  1826.         is represented as follows:
  1827.  
  1828.              COLOR          PALETTE 0           PALETTE 1
  1829.              
  1830.                0            background          background
  1831.                1            green               cyan
  1832.                2            red                 magenta
  1833.                3            brown               white
  1834.  
  1835.         To change the palette and the background,  see the IBM  Technical 
  1836.         Reference Bios listings for more information.
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.                                        28
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.                                    Trace Files
  1853.  
  1854.                                  (type ED only)
  1855.  
  1856.              You  can  now dump your trace to disk,  instead  of  (groan) 
  1857.         wasting  reams of printer paper.  This option is described in the 
  1858.         next section.
  1859.  
  1860.  
  1861.                                The Interrupt Menu
  1862.  
  1863.                                  (type ED only)
  1864.  
  1865.              This  menu  has been modified.  It was formerly  called  the 
  1866.         ESCAPE   menu,  but  the  meaning  of the  ESCAPE  key  has  been 
  1867.         redefined.  It  is  no  longer necessary to display the  menu  to 
  1868.         perform  one of the menu functions.  This reduces the  amount  of 
  1869.         display which is lost by scrolling off the screen.
  1870.  
  1871.              Version  1.9 offers the ability to attempt a goal  from  the 
  1872.         interrupt menu, while not disturbing the ongoing computation.
  1873.  
  1874.              At any time while searching, printing, or accepting keyboard 
  1875.         input,  you can break to this menu.  It is generally not possible 
  1876.         to  do  this  during disk access,  since control  passes  to  the 
  1877.         operating  system  at this time.  Two keys cause  this  break  to 
  1878.         occur:
  1879.  
  1880.              ^V:  The menu is displayed and a command is accepted at the 
  1881.                   prompt  "INTERRUPT>".  After  a command,  the  menu  is 
  1882.                   redisplayed  until  the user selects  a  command  which 
  1883.                   causes an exit. 
  1884.  
  1885.              ^I:  The menu is not displayed.  Command is accepted at the 
  1886.                   prompt  "INTERRUPT>" until the user selects  a  command 
  1887.                   which causes an exit.
  1888.  
  1889.              ESC: Typing this key  causes a  termination of  the  PROLOG      
  1890.                   search  and  control returns to the user command  level      
  1891.                   with a prompt of "?-".  Notice that previously, the ESC      
  1892.                   key invoked this menu. 
  1893.  
  1894.  
  1895.  
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.  
  1902.  
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908.  
  1909.                                        29
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.              As the resulting menu indicates, the following functions are 
  1919.         possible:
  1920.  
  1921.              A: Abort the search and return to the prompt.
  1922.  
  1923.              O  Open  a trace file.  The user is prompted for  the  file 
  1924.                 name.  The file receives all trace output.  If a file is 
  1925.                 already opened it is closed with all output preserved.
  1926.  
  1927.              C  Close the trace file if one is open.  Otherwise there is 
  1928.                 no effect.
  1929.  
  1930.             ^C: Immediately  exit PROLOG without closing files.  This is 
  1931.                 not advised.
  1932.  
  1933.             ^P: Typing <Control>P toggles the printer. If the printer is 
  1934.                 on,  all  input  and output will also be  routed  to  the 
  1935.                 printer.
  1936.  
  1937.              S: If  the machine in use is an IBM PC compatible  machine, 
  1938.                 the  currently displayed screen will be printed.  If  the 
  1939.                 machine  is  not an IBM PC compatible,  do not  use  this 
  1940.                 function.
  1941.  
  1942.              T: If  trace  is in use,  most of the trace output  can  be 
  1943.                 temporarily turned off by use of this function,  which is 
  1944.                 a toggle.
  1945.  
  1946.              G: Satisfy a goal.  The system prompt will appear. Do not be         
  1947.                 confused  into  thinking that the system has reverted  to 
  1948.                 the  prompt.  After the user has entered the goal in  the 
  1949.                 usual  manner,  satisfaction  of the goal is  immediately 
  1950.                 attempted.  This need not affect the running  computation 
  1951.                 in  any way,  although it is certainly possible to do so. 
  1952.                 After  the  goal is attempted,  control  returns  to  the 
  1953.                 INTERRUPT  menu.  The  purpose is to facilitate  detailed 
  1954.                 analysis of a running system.
  1955.  
  1956.              R:  Entering  another  ESC causes a return to the  current 
  1957.                 activity  (keyboard  input or search)  with  no  residual 
  1958.                 effect from the interruption.
  1959.  
  1960.  
  1961.  
  1962.  
  1963.  
  1964.  
  1965.  
  1966.  
  1967.  
  1968.  
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.                                        30
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.  
  1985.                              Conserving memory space
  1986.  
  1987.  
  1988.                Success popping is controlled by the predicates "popond",
  1989.         "popoffd",  "popon",  and  "popoff".  Success popping is means of 
  1990.         reclaiming  storage which is used on backtracking to  reconstruct 
  1991.         how a particular goal was satisfied.  If it is obvious that there 
  1992.         is no alternative solution to a goal this PROLOG system is  smart 
  1993.         enough to reclaim that storage. 
  1994.  
  1995.              In  this system,  succees popping is an expensive operation. 
  1996.         Therefore,  there  is a tradeoff of memory versus  time.  On  the 
  1997.         other hand, discrete use of success popping can actually speed up 
  1998.         a program by recreating structures in a more accessible form.
  1999.  
  2000.              The  definitions of the control predicates is given in  this 
  2001.         manual  and  their use is totally  optional.  The  modulation  of 
  2002.         success popping has no effect on program logic (read solution.)
  2003.              
  2004.              The  "cut"  can  save  substantial  time  and  computational 
  2005.         overhead  as well as storage.  Although the execution of the  cut 
  2006.         costs  time,  you can design your program to use cuts in critical 
  2007.         places  to  avoid unnecessary backtracking.  Thus  the  execution 
  2008.         speed of the program can actually increase. 
  2009.              
  2010.              Anyone  who  has  read Clocksin and  Mellish  is  aware,  of 
  2011.         course, that the "cut" has a powerful logical impact which is not 
  2012.         always desirable.
  2013.  
  2014.  
  2015.         popoff 
  2016.  
  2017.         See the below definition.
  2018.  
  2019.  
  2020.  
  2021.         popon
  2022.  
  2023.         The  inference  engine  does complete success popping  for  goals 
  2024.         which appear after "popon".  Consider this example: 
  2025.  
  2026.           goal :-  a, popon, b, c, popoff, d.
  2027.  
  2028.         If  no alternative solutions exist for b,  then  success  popping 
  2029.         will  reclaim storage by removing unnecessary records  describing 
  2030.         how  "b"  was  satisfied.  If the Prolog system cannot  rule  out 
  2031.         possible additional solutions,  success popping will never occur, 
  2032.         regardless of your use of "popon". 
  2033.              Since goal "d" occurs after "popoff",  success popping  will 
  2034.         never occur.
  2035.  
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.                                        31
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.         popoffd
  2051.  
  2052.         If  no  "popon" or "popoff" declarations occur in a  clause,  the 
  2053.         default  action  is  determined by  "popoffd"  and  "popond".  If 
  2054.         "popoffd" has been invoked,  the default is that success  popping 
  2055.         will not occur.
  2056.  
  2057.  
  2058.  
  2059.         popond
  2060.  
  2061.         The inverse of "popoffd". Turns on default success popping.
  2062.         printf( <stream>, <term1>,<term2>,... )
  2063.  
  2064.  
  2065.  
  2066.  
  2067.  
  2068.  
  2069.  
  2070.  
  2071.  
  2072.  
  2073.  
  2074.  
  2075.  
  2076.  
  2077.  
  2078.  
  2079.  
  2080.  
  2081.  
  2082.  
  2083.  
  2084.  
  2085.  
  2086.  
  2087.  
  2088.  
  2089.  
  2090.  
  2091.  
  2092.  
  2093.  
  2094.  
  2095.  
  2096.  
  2097.  
  2098.  
  2099.  
  2100.  
  2101.  
  2102.  
  2103.  
  2104.  
  2105.  
  2106.  
  2107.                                        32
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.                                  Prolog Tutorial          
  2117.  
  2118.  
  2119.                                   Introduction
  2120.  
  2121.  
  2122.  
  2123.  
  2124.              Probably  you  have heard of the language PROLOG within  the 
  2125.         last year or so. You probably wondered the following things:
  2126.  
  2127.         1) What does the name stand for? Names of computer languages are 
  2128.         almost always acronyms.
  2129.  
  2130.         2) What is it good for?
  2131.  
  2132.         3) Why now?
  2133.  
  2134.         4) Can I get a copy to play with?
  2135.  
  2136.              Congratulations! You obviously know the answer to the fourth 
  2137.         question. We now respond to the other three.
  2138.              
  2139.         1)  The  name  stands for "programming in logic." This  we  shall 
  2140.         elaborate on in depth later on.
  2141.  
  2142.         2) PROLOG is good for writing question answering systems.  It  is 
  2143.         also   good   for  writing  programs  that  perform   complicated 
  2144.         strategies  that  compute the best or worst way to  accomplish  a 
  2145.         task, or avoid an undesirable result.
  2146.  
  2147.         3) PROLOG was virtually unknown in this country until researchers 
  2148.         in  Japan announced that it was to be the core language  of  that 
  2149.         country's fifth generation computer project.  This is the project 
  2150.         with  which  Japan hopes to achieve a domainant position  in  the 
  2151.         world information industry of the 1990's. 
  2152.  
  2153.              PROLOG  is  one of the most unusual computer languages  ever 
  2154.         invented.  It  cannot be compared to  FORTRAN,  PASCAL,  "C",  or 
  2155.         BASIC.  The facilities complement,  rather than replace those  of 
  2156.         conventional  languages.  Although  it  has great  potential  for 
  2157.         database  work,  it  has  nothing in  common  with  the  database 
  2158.         languages used on microcomputers.
  2159.  
  2160.              Perhaps  the  best point to make is that while  conventional 
  2161.         languages are prescriptive, PROLOG is descriptive. A statement in 
  2162.         a conventional language might read:
  2163.  
  2164.              if( car_wheels = TRUE ) then
  2165.                begin
  2166.                  (some sort of procedure)
  2167.                  X = X + 1;
  2168.                end 
  2169.  
  2170.  
  2171.  
  2172.  
  2173.                                        33
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.         A statment in PROLOG could just be a statment of fact about  cars 
  2183.         and wheels. There are many relationships that hold. For instance,
  2184.  
  2185.              has( car, wheels ).
  2186.  
  2187.              has( car, quant(wheels, four) ).
  2188.  
  2189.              round( wheels ).
  2190.  
  2191.         Each  of  these statments is an independent fact  relating  cars, 
  2192.         wheels,  and  the  characteristics of wheels.  Because  they  are 
  2193.         independent, they can be put into a PROLOG program by programmers 
  2194.         working separately. The man who is a specialist on car bodies can 
  2195.         say  his thing,  the wheel specialist can have his say,  and  the 
  2196.         participants can work with relative independence. And this brings 
  2197.         to light a major advantage of PROLOG:
  2198.  
  2199.  
  2200.                              PARALLEL PROGRAMMING!!!
  2201.                             
  2202.  
  2203.         With  conventional  programming languages projects can  still  be 
  2204.         "chunked",  or  divided between programmers.  But efficiency on a 
  2205.         team  project  drops  drastically below that  of  the  individual 
  2206.         programmer  wrapped  up  in  his own trance.  As  the  number  of 
  2207.         participants    grows   the   need   for   communication    grows 
  2208.         geometrically. The time spent communicating can exceed that spent 
  2209.         programming! 
  2210.              Although   PROLOG   does   not  eliminate   the   need   for 
  2211.         task  coordination,  the problem is considerably  simplified.  It 
  2212.         also provides the ability to answer questions in a "ready to  eat 
  2213.         form."  Consider your favorite BASIC interpreter.  Based upon the 
  2214.         statements about cars and wheels previously given,  could you ask 
  2215.         it the following question?   
  2216.  
  2217.                        
  2218.               has( car, X ), round( X ).
  2219.  
  2220.              Does  a  car  have anything which  is  round?  The  question 
  2221.         instructs the PROLOG interpreter to consider all the objects that 
  2222.         it  knows are possessed by a car and find those which are  round. 
  2223.         Perhaps  you are beginning to guess that PROLOG has the abilities 
  2224.         of a smart database searcher.  It can not only find the facts but 
  2225.         selectively find them and interpret them.
  2226.  
  2227.  
  2228.  
  2229.  
  2230.  
  2231.  
  2232.  
  2233.  
  2234.  
  2235.  
  2236.  
  2237.  
  2238.  
  2239.                                        34
  2240.  
  2241.  
  2242.  
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248.              Consider the problem of a fault tree, as exemplified by this 
  2249.         abbreviated one:
  2250.  
  2251.  
  2252.  
  2253.         {Car won't start}
  2254.              | 
  2255.              | 
  2256.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  2257.                 (Yes)                                       v
  2258.                  |                                  {Check battery}
  2259.                  |
  2260.         [Smell gasoline](yes) --> {Try full throttle cranking}
  2261.                  |                       (failure)
  2262.         /--------/                           |
  2263.  
  2264.                             (details omitted)
  2265.  
  2266.  
  2267.  
  2268.              The fault tree is easily programmed in BASIC. Later we shall 
  2269.         show  that  PROLOG supplies a superior replacement for the  fault 
  2270.         tree.  Though the tree is capable of diagnosing only the  problem 
  2271.         for  which  it was designed,  PROLOG dynamically  constructs  the 
  2272.         appropriate  tree from facts and rules you have provided.  PROLOG 
  2273.         is not limited to answering one specific question.  Given  enough 
  2274.         information,  it  will attempt to find all deductive solutions to 
  2275.         any problem.
  2276.  
  2277.  
  2278.  
  2279.  
  2280.  
  2281.  
  2282.  
  2283.  
  2284.  
  2285.  
  2286.  
  2287.  
  2288.  
  2289.  
  2290.  
  2291.  
  2292.  
  2293.  
  2294.  
  2295.  
  2296.  
  2297.  
  2298.  
  2299.  
  2300.  
  2301.  
  2302.  
  2303.  
  2304.  
  2305.                                        35
  2306.  
  2307.  
  2308.  
  2309.  
  2310.  
  2311.  
  2312.  
  2313.  
  2314.  
  2315.                                   PROLOG PRIMER
  2316.  
  2317.         I.                       Rules and Facts     
  2318.  
  2319.  
  2320.  
  2321.              This  is  where you should start if you know  nothing  about 
  2322.         PROLOG. Let us consider a simple statment in PROLOG, such as:
  2323.  
  2324.         1)   has( car, wheels ).
  2325.  
  2326.         This  statement  is a "fact.  The word "has" in this statment  is 
  2327.         known  either  as a functor or predicate.  It is a name  for  the 
  2328.         relationship  within the parenthesis.  It implies that a car  has 
  2329.         wheels.  But  the  order  of  the words  inside  the  bracket  is 
  2330.         arbitrary and established by you. You could just as easily say:
  2331.  
  2332.         2)   has( wheels, car ).
  2333.  
  2334.         and if you wrote this way consistently,  all would be  well.  The 
  2335.         words  has,  wheels,  and car are all PROLOG atoms.  "Wheels" and 
  2336.         "car" are constants. 
  2337.              
  2338.         A   database   of  facts  can  illustrate  the   data   retrieval 
  2339.         capabilities of PROLOG. For instance:
  2340.  
  2341.         3)   has( car, wheels ).
  2342.              has( car, frame ).
  2343.              has( car, windshield ).
  2344.              has( car, engine ).
  2345.  
  2346.         You could then ask PROLOG the question:
  2347.  
  2348.         4)   has( car, Part ).
  2349.  
  2350.         The  capital  "P" of Part means that Part is a  variable.  PROLOG 
  2351.         will make Part equal to whatever constant is required to make the 
  2352.         question match one of the facts in the database. Thus PROLOG will 
  2353.         respond:
  2354.  
  2355.              Part = wheels.
  2356.              
  2357.              More?(Y/N):
  2358.  
  2359.         If you type "y" the next answer will appear:
  2360.  
  2361.              Part = frame.
  2362.  
  2363.              More?(Y/N):
  2364.  
  2365.         If you continue, PROLOG will produce the answers Part = windshield 
  2366.         and Part = engine. Finally, you will see:
  2367.  
  2368.              More?(Y/N):y
  2369.  
  2370.  
  2371.                                        36
  2372.  
  2373.  
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380.  
  2381.              No.
  2382.          
  2383.         indicating that PROLOG has exhausted the database.  Incidentally, 
  2384.         when  a  variable is set equal to a constant or  other  variable, 
  2385.         it is said to be instantiated to that object.
  2386.  
  2387.              Notice  that  PROLOG searches the database forwards  and  in 
  2388.         this case,  from the beginning.  The forward search path is built 
  2389.         into PROLOG and cannot be changed. An author of a program written 
  2390.         in  a  prescriptive language is quite conscious of the  order  of 
  2391.         execution  of  his program,  while in PROLOG it is  not  directly 
  2392.         under his control.
  2393.              
  2394.              The other major element is the rule which is a fact which is 
  2395.         conditionally true. In logic this is called a Horn clause: 
  2396.  
  2397.  
  2398.         5)   has( X, wheels ) :- iscar( X ).
  2399.  
  2400.         The  fact iscar( car ) and the above rule are equivalent to
  2401.  
  2402.         6)   has( car, wheels).
  2403.  
  2404.         The  symbol :- is the "rule sign." The expression on the left  of 
  2405.         :-is the "head" and on the right is the body.  The variable X has 
  2406.         scope  of the rule,  which means that it has meaning only  within 
  2407.         the rule.  For instance,  we could have two rules in the database 
  2408.         using identically named variables.
  2409.  
  2410.  
  2411.         7)   has( X,  transportation ) :- 
  2412.                            has( X,  car ), has( license, X ).
  2413.  
  2414.         8)   has( X, elephant ) :- istrainer( X ), hasjob( X ).
  2415.  
  2416.         The  variables  X in the two expressions are completely  distinct 
  2417.         and have nothing to do with each other.
  2418.  
  2419.         The comma between has( X, car ) and has( license, X ) means "and" 
  2420.         or logical conjuction.  The rule will not be true unless both the 
  2421.         clauses has(X, car) and has( license, X ) are true.
  2422.  
  2423.  
  2424.              On the other hand if there is a rule
  2425.              
  2426.         9)   has( license, X ) :- passedexam( X ).
  2427.  
  2428.         consider what PROLOG will do in response to the question:
  2429.  
  2430.         10)  has( harry, transportation ).
  2431.  
  2432.         (Notice  that  harry has not been capitalized because we  do  not 
  2433.         want  it  taken as a variable.  We could,  however,  say  'Harry' 
  2434.         enclosed in single quotes.)
  2435.  
  2436.  
  2437.                                        37
  2438.  
  2439.  
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.  
  2447.              It  will scan the database and use (7),  in which X will  be 
  2448.         instantiated to harry. The rule generates two new questions:
  2449.  
  2450.         11)  has( harry, car ).
  2451.  
  2452.         12)  has( license, harry ).
  2453.  
  2454.         Assuming  that  harry  has  a car,  the first clause  of  (7)  is 
  2455.         satisfied and the database is scanned for a match to (12). PROLOG 
  2456.         picks  up  rule (9) in which X is instantiated to harry  and  the 
  2457.         question is now posed:
  2458.  
  2459.         13)  passedexam( harry ).
  2460.  
  2461.              If there is a fact:
  2462.  
  2463.              passedexam( harry ).
  2464.  
  2465.         in  the database then all is well and harry  has  transportation. 
  2466.         If there is not, then PROLOG will succinctly tell you:
  2467.  
  2468.              No.
  2469.  
  2470.         But  suppose Harry has money and can hire a chauffer as any  good 
  2471.         programmer  can.  That  could be made part of the program in  the 
  2472.         following way.
  2473.  
  2474.              The rule which PROLOG tried to use was:
  2475.  
  2476.         14)  has( X,  transportation ) :- 
  2477.                            has( X,  car ), has( license, X ).
  2478.  
  2479.         At any point following it there could be included another rule:
  2480.  
  2481.         15)  has( X, transportation ) :- has( X, money ).
  2482.  
  2483.         or simply the bald fact:
  2484.  
  2485.         16)  has( harry, transportation ).
  2486.  
  2487.              These  additional  rules  or  facts would  be  used  in  two 
  2488.         circumstances.  If at any point a rule does not yield a solution, 
  2489.         PROLOG   will  scan  forward  from  that  rule  to  find  another 
  2490.         applicable  one.  This process is known as "backtracking  search" 
  2491.         and can be quite time consuming.
  2492.  
  2493.  
  2494.         If  in response to the "More?" prompt you answer "y" PROLOG  will 
  2495.         search  for an additional distinct solution.  It will attempt  to 
  2496.         find an alternate rule or fact for the last rule or fact used. If 
  2497.         that  fails,  it  will back up to the antecedent rule and try  to 
  2498.         find  an alternate antecedent.  And it will continue to  back  up 
  2499.         until  it  arrives at the question you asked,  at which point  it 
  2500.         will say:
  2501.  
  2502.  
  2503.                                        38
  2504.  
  2505.  
  2506.  
  2507.  
  2508.  
  2509.  
  2510.  
  2511.  
  2512.  
  2513.              No.
  2514.  
  2515.         "Antecedent"  to a rule means that it gave rise to its' use.  For 
  2516.         example,  (7)  is  the antecedent of (9) in the  context  of  the 
  2517.         question (16).
  2518.  
  2519.  
  2520.  
  2521.  
  2522.         II.                          Grammar
  2523.  
  2524.              It is a boring subject, but it must be discussed. All PROLOG 
  2525.         statements are composed of valid terms, possibly a rule sign (":-
  2526.         "),  commas representing conjunction ("and"), and a period at the 
  2527.         very end.
  2528.              A term is a structure, constant, variable, or number.
  2529.          
  2530.              What is a structure? It is a kind of grouping:
  2531.  
  2532.              1) Structures consist of a functor, and a set of objects or
  2533.                 structures in parenthesis.
  2534.  
  2535.              2)  Objects are constants,  variables,  numbers,  or  lists, 
  2536.                 which we have not discussed yet.
  2537.  
  2538.              3)  A  constant or functor must be a string of  letters  and 
  2539.                 numbers, beginning with a lower case letter, unless
  2540.                 you  choose  to  enclose  it in single  quotes  (  'howdy 
  2541.                 pardner'  ),  in  which  case you are  freed  from  these 
  2542.                 restrictions.
  2543.              4) A  variable  must be a string of  letters  and  numbers 
  2544.                 beginning with a capital letter.
  2545.              
  2546.              5) A  functor  may optionally have  arguments  enclosed  in 
  2547.                 parenthesis , as in: hascar( X ) or hascar. 
  2548.  
  2549.         An example:  "has( X, transportation )." is a structure.
  2550.  
  2551.  
  2552.  
  2553.  
  2554.  
  2555.  
  2556.  
  2557.  
  2558.  
  2559.  
  2560.  
  2561.  
  2562.  
  2563.  
  2564.  
  2565.  
  2566.  
  2567.  
  2568.  
  2569.                                        39
  2570.  
  2571.  
  2572.  
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578.         III.                     Input / Output      
  2579.  
  2580.              You   now   know  enough  to  write  simple  databases   and 
  2581.         interrogate   them  profitably.   But  before  we  examine   more 
  2582.         sophisticated  examples,  it  will be necessary to add input  and 
  2583.         output to the language. There are built in functions which appear 
  2584.         as rules which are satisfied once. Thus the statment:
  2585.  
  2586.              write( 'Hello world.' ).
  2587.  
  2588.         can be included on the right side of a rule:
  2589.  
  2590.  
  2591.         greetings(  X ) :- ishuman( X ),  write( 'Hello world.' ).  You 
  2592.         can  also write "write( X )" where X is some variable.  Note that 
  2593.         'Hello  world.' is not enclosed in double quotes.  Single quotes, 
  2594.         which denote a constant, are required. Double quotes would denote 
  2595.         a list, which is another thing entirely.
  2596.  
  2597.         Provided  that  a match to "ishuman" can be  found,  the  builtin 
  2598.         function  "write"  is  executed and the message  printed  to  the 
  2599.         screen.
  2600.              The  builtin  read( X ) reads a "structure" that  you  input 
  2601.         from the keyboard. More formally, we have
  2602.  
  2603.              read( <variable> or <constant> )
  2604.              write( <variable> or <constant> )
  2605.  
  2606.         If you write read( Input ),  then the variable "keyboard" will be 
  2607.         assigned to whatever is typed at the keyboard,  provided that the 
  2608.         input  is a valid PROLOG structure.  The builtin "read" will fail 
  2609.         if instead of Keyboard we wrote read( baloney ),  where "baloney" 
  2610.         is a constant,  and the user at the keyboard did not type exactly 
  2611.         "baloney." 
  2612.  
  2613.         When you input a structure in response to a "read" statement,  be 
  2614.         sure to end it with a period and an <ENTER>. 
  2615.  
  2616.              There  is  a convenient way of putting the cursor on  a  new 
  2617.         line. This is the builtin "nl". For example:
  2618.  
  2619.              showme :- write( 'line 1' ), nl, write( 'line 2' ). 
  2620.  
  2621.         would result in:
  2622.  
  2623.              line 1
  2624.              line 2
  2625.  
  2626.              There  is  also a primitive form of input/output for  single 
  2627.         characters. It will be discussed later.
  2628.  
  2629.  
  2630.  
  2631.  
  2632.  
  2633.  
  2634.  
  2635.                                        40
  2636.  
  2637.  
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644.         IV.                   A Fault Tree Example
  2645.  
  2646.              Consider the "won't start" fault tree for an automobile:
  2647.  
  2648.         {Car won't start}
  2649.              | 
  2650.              | 
  2651.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  2652.                 (Yes)                                       v
  2653.                  |                                  {Check battery}
  2654.                  |
  2655.         [Smell gasoline](yes) --> {Try full throttle cranking}
  2656.                  |                       (failure)
  2657.         /--------/                           |
  2658.         |           /------------------------/ 
  2659.         |           |                       
  2660.         |           |
  2661.         |  [Check for fuel line leaks](yes)-->{Replace fuel line}
  2662.         |          (no)
  2663.         |           |
  2664.         |           |
  2665.         |  [Check for defective carburator](yes)--\
  2666.         |          (no)                           v
  2667.         |                                {Repair carburator}
  2668.         \----\
  2669.              |
  2670.              |
  2671.         [Is spark present](no)-->[Do points open and close](no)-\
  2672.              |                             (yes)                v
  2673.         /----/                               |          {Adjust points}
  2674.         |           /------------------------/
  2675.         |           |
  2676.         |  [Pull distributor wire, observe spark](blue)--\
  2677.         |        (orange)                                v
  2678.         |           |                           {Check plug wires & cap}
  2679.         |           |
  2680.         |  [Measure voltage on coil primary](not 12V)--\
  2681.         |         (12V)                                v
  2682.         |           |              {Check wiring, ballast resistor}
  2683.         |           |
  2684.         |  [Check condenser with ohmmeter](conducts)--\
  2685.         |    (no conduction)                          v
  2686.         |           |                         {Replace condenser}
  2687.         |           |
  2688.         |  [Open and close points](voltage not 0 - 12)--\
  2689.         |   (voltage swings 0 - 12)                     v
  2690.         |           |                         {Fix primary circuit}
  2691.         |           |
  2692.         |  {Consider hidden fault, swap components]
  2693.         |
  2694.         |
  2695.         \-------{Call a tow truck!!}
  2696.  
  2697.  
  2698.  
  2699.  
  2700.  
  2701.                                        41
  2702.  
  2703.  
  2704.  
  2705.  
  2706.  
  2707.  
  2708.  
  2709.  
  2710.              A PROLOG program to  implement this is simple. Each statment 
  2711.         represents  a  decision point fragment of the  tree.  The  PROLOG 
  2712.         interpreter  dynamically  assembles  the tree as  it  attempts  a 
  2713.         solution. 
  2714.  
  2715.         'car wont start' :- write( 'Is the battery voltage low?' ), 
  2716.                             affirm, nl,
  2717.                             write( 'Check battery' ).
  2718.  
  2719.         'car wont start' :- write( 'Smell gasoline?' ), 
  2720.                             affirm, nl,
  2721.                             'fuel system'.
  2722.  
  2723.         'fuel system'    :- write( 'Try full throttle cranking' ).
  2724.  
  2725.         'fuel system'    :- write( 'Are there fuel line leaks?' ),
  2726.                             affirm, nl,
  2727.                             write( 'Replace fuel line.' ).
  2728.  
  2729.         'fuel system'    :- write( 'Check carburator' ).
  2730.  
  2731.         'car wont start' :- write( 'Is spark present?' ),
  2732.                             not( affirm ), nl,
  2733.                             'no spark'.
  2734.  
  2735.         'no spark'       :- write( 'Do points open and close?' ),
  2736.                             not( affirm ), nl,
  2737.                             write( 'Adjust or replace points.' ).
  2738.  
  2739.         'no spark'       :- write( 'Is the spark off the coil good?' ),
  2740.                             affirm,
  2741.                             write( 'Check plug wires and cap.' ).
  2742.  
  2743.         'no spark'       :- write( 'What is the voltage on the primary
  2744.                              of the coil: ' ), 
  2745.                             read( Volts ), 
  2746.                             Volts < 10,
  2747.                             nl,
  2748.                             write('Check wiring and ballast resistor.').
  2749.  
  2750.         'no spark'       :- write( 'Does the capacitor leak?' ),
  2751.                             affirm,
  2752.                             write( 'Replace the capacitor.' ).
  2753.  
  2754.         'no spark'       :- not( 'primary circuit' ).
  2755.  
  2756.         'primary circuit' 
  2757.                          :- write( 'Open the  points.  Voltage  across 
  2758.                               coil?:'), nl,
  2759.                             read( Openvolts ), Openvolts < 1, 
  2760.                             write(  'Close the points.  Voltage  across 
  2761.                               coil?:'),
  2762.                             read( Closevolts ), Closevolts > 10, nl,
  2763.                             write( 'Primary circuit is OK.' ). 
  2764.  
  2765.  
  2766.  
  2767.                                        42
  2768.  
  2769.  
  2770.  
  2771.  
  2772.  
  2773.  
  2774.  
  2775.  
  2776.         'no spark'       :- write( 'Consider a hidden fault. Swap
  2777.                               cap, rotor,points,capacitor.' ).
  2778.  
  2779.  
  2780.         'Car wont start' :- write( 'Get a tow truck!!' ).
  2781.  
  2782.  
  2783.                                  --End program--
  2784.  
  2785.  
  2786.              The  above  is  a  simple example of  an  expert  system.  A 
  2787.         sophisticated  system would tell you exactly the method by  which 
  2788.         it  has reached a conclusion.  It would communicate by a  "shell" 
  2789.         program  written  in PROLOG which would accept a wider  range  of 
  2790.         input   than  the  "valid  structure"  required  by  the   PROLOG 
  2791.         interpreter directly.
  2792.  
  2793.  
  2794.  
  2795.  
  2796.  
  2797.  
  2798.  
  2799.  
  2800.  
  2801.  
  2802.  
  2803.  
  2804.  
  2805.  
  2806.  
  2807.  
  2808.  
  2809.  
  2810.  
  2811.  
  2812.  
  2813.  
  2814.  
  2815.  
  2816.  
  2817.  
  2818.  
  2819.  
  2820.  
  2821.  
  2822.  
  2823.  
  2824.  
  2825.  
  2826.  
  2827.  
  2828.  
  2829.  
  2830.  
  2831.  
  2832.  
  2833.                                        43
  2834.  
  2835.  
  2836.  
  2837.  
  2838.  
  2839.  
  2840.  
  2841.  
  2842.         V.                            Lists               
  2843.  
  2844.              Consider  a  shopping list given you by your wife.  It is  a 
  2845.         piece of paper with items written on it in an order that probably 
  2846.         symbolizes  their  importance.  At the top it  may  say  EGGS!!!, 
  2847.         followed by carrots, hamburger, and finally a flea collar for the 
  2848.         dog, if you can find one. In PROLOG such a list would be written:
  2849.  
  2850.         1)   [eggs, carrots, hamburger, fleacollar]
  2851.  
  2852.         The  order of a list is important so that eggs and carrots cannot 
  2853.         be reversed and PROLOG be uncaring.
  2854.  
  2855.         Let us put the list in a structure:
  2856.  
  2857.              shopping( [eggs, carrots, hamburger, fleacollar] ).
  2858.  
  2859.         Then  if you wished to isolate the head of the list you could ask 
  2860.         the question:
  2861.  
  2862.              shopping( [ Mostimportant | Rest ] ).
  2863.  
  2864.         and PROLOG would respond:
  2865.  
  2866.              Mostimportant   =  eggs,   
  2867.              Rest   =   [carrots,   hamburger, fleacollar].
  2868.  
  2869.         The vertical bar "|" is crucial here. It is the string extraction 
  2870.         operator,  which  performs  a  combination  of the  CDR  and  CAR 
  2871.         functions  of LISP.  When it appears in the context [X|Y] it  can 
  2872.         separate the head of the list from the rest, or tail.
  2873.  
  2874.  
  2875.              You  may have gained the impression that PROLOG is a  rather 
  2876.         static language capable of answering simple questions,  but it is 
  2877.         far  more powerful than that.  The string extraction operator  is 
  2878.         the  key.  It permits PROLOG to whittle a complex expression down 
  2879.         to the bare remainder.  If the rules you have given it permit  it 
  2880.         to  whittle  the  remainder  down to  nothing,  then  success  is 
  2881.         achieved. An example of this is the definition of "append."
  2882.  
  2883.              Let  us suppose you have not yet done yesterday's  shopping, 
  2884.         let alone today's. You pull it out of your wallet and sootch tape 
  2885.         it to the list your wife just gave you. Yesterday's list was:
  2886.  
  2887.              [tomatoes, onions, ketchup]
  2888.  
  2889.         Combined with [eggs, carrots, hamburger, fleacollar] we obtain
  2890.  
  2891.              [eggs,carrots,hamburger,fleacollar,tomatoes,onions,garlic].
  2892.  
  2893.         To  take one list and to attach it to the tail of another list is 
  2894.         to  "append"  the first to the second.  The PROLOG definition  of 
  2895.         append is:
  2896.  
  2897.  
  2898.  
  2899.                                        44
  2900.  
  2901.  
  2902.  
  2903.  
  2904.  
  2905.  
  2906.  
  2907.  
  2908.  
  2909.  
  2910.         Rule1:     append( [], L, L ).
  2911.  
  2912.         Rule2:     append( [X|List1], List2, [X|List3] ) :-
  2913.                       append( List1, List2, List3 ].
  2914.  
  2915.         The  general  scheme is this:  
  2916.  
  2917.         The definition consists of one rule and one fact.  The rule  will 
  2918.         be used over and over again until what little is left matches the 
  2919.         fact.  The [] stands for empty list,  which is like a bag without 
  2920.         anything in it. This is an example of a recursive definition.
  2921.              Suppose we ask:
  2922.  
  2923.              append( [a,b,c], [d,e,f], Whatgives ).
  2924.  
  2925.         1. Rule 2 is invoked with arguments ( [a,b,c], [d,e,f], Whatgives ).
  2926.         2. Rule 2 is invoked again with arguments:
  2927.              ( [b,c], [d,e,f], List3 ).
  2928.         3. Rule 2 is invoked again with arguments:
  2929.              ( [b], [d,e,f], List3 ).
  2930.         4.  The  arguments  are now ([],  [d,e,f],  List3 ).  Rule 1  now 
  2931.             matches. End.
  2932.  
  2933.         How does this cause a list to be constructed? The key is to watch 
  2934.         the   third  argument.   Supplied  by  the  user,   it  is  named 
  2935.         "Whatgives". The inference engine matches it to [X|List3] in rule 
  2936.         2. Now lets trace this as rule two is successivly invoked: 
  2937.  
  2938.  
  2939.                 Whatgives                                                
  2940.                    |                                                     
  2941.                    |                                                     
  2942.                    |                                                     
  2943.                    v                                                     
  2944.         Rule2:  [X|List3] (List1 = [b,c])                                
  2945.                  |  \                                                    
  2946.                  |   \                                                   
  2947.                  |    \                                                  
  2948.                  v     \                                                 
  2949.         Rule2:   a   [X'|List3'] (List1' = [c])                          
  2950.                       |    \                                             
  2951.                       |     \                                            
  2952.                       |      \                                           
  2953.                       v       \                                          
  2954.         Rule2:        b     [X''|List3''] (List1'' = [], ie., empty set.)
  2955.                               |    \                                     
  2956.                               |     \                                    
  2957.                               |      \                                   
  2958.         Rule1:                c       L  ( in Rule1 = [d,e,f] )              
  2959.                                                                          
  2960.         End.
  2961.  
  2962.  
  2963.  
  2964.  
  2965.                                        45
  2966.  
  2967.  
  2968.  
  2969.  
  2970.  
  2971.  
  2972.  
  2973.  
  2974.         L in rule 1 is [d,e,f] for the following reason: Notice that rule 
  2975.         2 never alters List2. It supplies it to whatever rule it invokes. 
  2976.         So L in rule 1 is the original List2, or [a,b,c].
  2977.  
  2978.              This example would not have worked if the order of rules one 
  2979.         and  two  were  reversed.  The  PROLOG  inference  engine  always 
  2980.         attempts to use the the first rule encountered. You could imagine 
  2981.         it as always reading your program from the top down in attempting 
  2982.         to  find an appropriate rule.  Since rule 2 would always satisfy, 
  2983.         an  unpleasant  thing  would  have happened  if  the  order  were 
  2984.         reversed. The program would loop forever.
  2985.              
  2986.  
  2987.  
  2988.  
  2989.              I  hope  that  this tiny introduction to PROLOG  whets  your 
  2990.         appetite. You should now purchase the book
  2991.  
  2992.              Programming In Prolog
  2993.              W.F. Clocksin and C.S. Mellish
  2994.              Springer - Verlag
  2995.              Berlin,Heidelberg,New York. 1981,1984
  2996.  
  2997.  
  2998.  
  2999.  
  3000.  
  3001.  
  3002.  
  3003.  
  3004.  
  3005.  
  3006.  
  3007.  
  3008.  
  3009.  
  3010.  
  3011.  
  3012.  
  3013.  
  3014.  
  3015.  
  3016.  
  3017.  
  3018.  
  3019.  
  3020.  
  3021.  
  3022.  
  3023.  
  3024.  
  3025.  
  3026.  
  3027.  
  3028.  
  3029.  
  3030.  
  3031.                                        46
  3032.  
  3033.  
  3034.  
  3035.  
  3036.  
  3037.